package com.quanyan.ic.datacenter.dao.impl;

import com.github.ltsopensource.core.commons.utils.DateUtils;
import com.quanyan.ic.common.model.OrderMessage;
import com.quanyan.ic.datacenter.dao.PlaceDao;
import com.quanyan.ic.datacenter.dao.PlaceOrderDao;
import com.quanyan.ic.datacenter.mapper.icData.PlaceUnitOrderMapper;
import com.quanyan.ic.datacenter.mapper.icData.entity.PlaceUnitOrder;
import com.quanyan.ic.datacenter.mapper.icData.entity.PlaceUnitOrderExample;
import com.quanyan.ic.datacenter.model.PlaceUnitInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * Created by Administrator on 2017/8/21.
 */
@Repository
public class PlaceOrderDaoImpl implements PlaceOrderDao {

    @Autowired
    private PlaceUnitOrderMapper placeUnitOrderMapper;
    @Autowired
    private PlaceDao placeDao;



    @Override
    public void insert(OrderMessage orderMessage,PlaceUnitInfo unitInfo) {
        PlaceUnitOrder tbIcPlaceOrder = new PlaceUnitOrder();
        tbIcPlaceOrder.setOrderNo(orderMessage.getOrderNo());
        tbIcPlaceOrder.setSource(orderMessage.getSource());
        tbIcPlaceOrder.setOrderStatus(orderMessage.getOrderStatus());
        tbIcPlaceOrder.setPhoneNum(orderMessage.getPhoneNum());
        tbIcPlaceOrder.setPlaceId(orderMessage.getPlaceId());
        tbIcPlaceOrder.setPlaceUnitId(orderMessage.getSiteId());
        tbIcPlaceOrder.setPlaceUnitType(orderMessage.getSiteType().byteValue());
        tbIcPlaceOrder.setUserId(orderMessage.getUserId());
        tbIcPlaceOrder.setUseType(orderMessage.getUseType());

        tbIcPlaceOrder.setCityId(unitInfo.getCityId());
        tbIcPlaceOrder.setCityName(unitInfo.getCityName());
        tbIcPlaceOrder.setCategoryName(unitInfo.getCategoryName());
        tbIcPlaceOrder.setCategoryId(unitInfo.getCategoryId());
        tbIcPlaceOrder.setPlaceName(unitInfo.getPlaceName());
        tbIcPlaceOrder.setPlaceUnitName(unitInfo.getPlaceUnitName());

        Date startTime = DateUtils.convert(orderMessage.getStartTime());
        Date endTime = DateUtils.convert(orderMessage.getEndTime());
        Long duration = endTime.getTime() - startTime.getTime();
        tbIcPlaceOrder.setStartTime(startTime);
        tbIcPlaceOrder.setEndTime(endTime);
//        tbIcPlaceOrder.setConsumeDate(startTime);
//        tbIcPlaceOrder.setDuration(duration.intValue());
        tbIcPlaceOrder.setTicketcount(orderMessage.getTicketCount());

        placeUnitOrderMapper.insertSelective(tbIcPlaceOrder);
    }

    @Override
    public int isOrderByDateTime (Integer placeUnitId ,Date date){
        PlaceUnitOrderExample example = new PlaceUnitOrderExample();
        PlaceUnitOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPlaceUnitIdEqualTo(placeUnitId);
        criteria.andStartTimeGreaterThanOrEqualTo(date);
        criteria.andEndTimeLessThanOrEqualTo(date);
        return (int)placeUnitOrderMapper.countByExample(example);
    }

}
